import Mock from "mockjs";

// 自动引入模块
const modulesFiles = require.context("./modules", true, /\.ts$/);

const mocks = modulesFiles.keys().reduce((modues: any, item) => {
  return [...modues, ...modulesFiles(item).default];
}, []);

// 设置拦截ajax请求的响应时间
Mock.setup({
  timeout: "200-600",
});

// 遍历mock配置项
mocks.forEach((item: any) => {
  Mock.mock(item.url, (options: any) => {
    const { type, body } = options;
    if (item.type !== type.toLowerCase()) {
      return { code: 500, msg: `该接口不支持${type}方式` };
    } else {
      return item.response(JSON.parse(body));
    }
  });
});
